﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Controls;

namespace VAdvantage.Grids
{
    public class VDataGridTextColumn : DataGridBoundColumn, IDataGridColumn
    {
        private TextBlock textBlock = null;
        private GridField _field = null;
        private String _columnName = "";
        private VTextBox editingControl = null;
        private object currentCellValue;

        private TextToPwdConverter _converter = null;

        public string DataPropertyName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value.ToUpper();
                this.Binding = new System.Windows.Data.Binding(_columnName);
                SetConverter();
            }
        }

        private void SetConverter()
        {
            if (this.Binding != null && _field != null)
            {
                if (_field.IsEncryptedField())
                {
                    if (_converter ==  null)
                    {
                        _converter = new TextToPwdConverter();

                        this.Binding.Converter = _converter;
                    }
                }
            }
        }

        public Controls.VDataGrid DataGridView
        {
            get;
            set;
        }

        public GridField Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;
                SetConverter();
            }
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            if (cell.Content is VTextBox)
            {
                //editingControl.VetoableChangeListener -= new EventHandler(editingControl_VetoableChangeListener);

                currentCellValue = ((VTextBox)cell.Content).GetValue();
                _field.SetValue(currentCellValue);
                ((DataUtil.DataObject)dataItem).SetFieldValue(_columnName, currentCellValue);
                //currentBlock.Text = value == null ? "0" : value.ToString();
                //textBlock.Text = currentCellValue == null ? "" : currentCellValue.ToString();
                return textBlock;
            }

            TextBlock tb = Utility.AEnv.GetDataGridElement();

            if (Binding != null)
            {
                tb.SetBinding(TextBlock.TextProperty, Binding);
            }
            return tb;
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (editingControl == null)
            {
                editingControl = new VTextBox(_field.GetColumnName(), _field.IsMandatory(false), _field.IsReadOnly(),
                                              _field.IsEditable(false), _field.GetDisplayLength(), _field.GetFieldLength(),
                                              _field.GetVFormat(), _field.GetObscureType(), _field.IsEncryptedField() ? VTextBox.TextType.Password : VTextBox.TextType.Text, _field.GetDisplayType());

                editingControl.SetField(_field);
            }

            currentCellValue = ((DataUtil.DataObject)dataItem).GetFieldValue(_columnName);
            editingControl.SetValue(currentCellValue);

            textBlock = (TextBlock)cell.Content;

            return editingControl;
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {

            //if (editingEventArgs.OriginalSource is TextBlock)
            // {
            //textBlock = (TextBlock)editingEventArgs.OriginalSource;
            editingControl.EditingControlDataGridView = DataGridView;
            //}

            //editingControl.VetoableChangeListener += new EventHandler(editingControl_VetoableChangeListener);
            return editingElement;
        }


        public void Dispose()
        {
            textBlock = null;
            _field = null;
        }

        public bool ReadOnly
        {
            get;
            set;
        }
    }
}
